﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace CoHSIN.Controllers
{
    public class AJAXController : Controller
    {
        //
        // GET: /AJAX/

        public string Index()
        {
            return "";
        }

        [HttpPost]
        public string saveContent(string pageName, string tabName, string HTML )
        {
            Models.Page Page = Globals.getPage(pageName);
            Models.Tab Tab = Globals.getTab(Page, tabName);
            Dictionary<string,string> response = new Dictionary<string,string>();
            response.Add("HTML", "");

            if (Tab == null) return new JavaScriptSerializer().Serialize(response);

            HTML = Server.UrlDecode(HTML);

            Models.Content Content = Tab.Contents.FirstOrDefault();

            if (Content == null) {
                Content = new Models.Content()
                {
                    TabID=Tab.TabID,
                    ContentData=HTML,
                    ContentType="a"
                    
                };
                Globals.cohsinDB.Contents.InsertOnSubmit(Content);
            }
            else{
                Content.ContentData = HTML;
            };

            try {
                Globals.cohsinDB.SubmitChanges();
                response["HTML"] = HTML;
            }
            catch (Exception e) { };

            Globals.setCohsinDB();
            string sResponse = new JavaScriptSerializer().Serialize(response);
            return sResponse;


        }

    }
}
